package ArrayList;

import java.util.Arrays;

public class TextDemo {


    int[] elems;
    int UseSize;
    public TextDemo(){
        this.elems = new int[5];
        this.UseSize = 0;
    }
    //判断是否为空
    public boolean isEmpty(){

     return this.UseSize==0;
    }

    //判断数组是不是满了
    public void CheckUseSize(){
        if(this.UseSize==this.elems.length){
            this.elems = Arrays.copyOf(this.elems,2*this.UseSize);
            return ;
        }else{
            return ;
        }
    }

    // 打印顺序表
    public void display() {
        if (isEmpty()) {
          throw new ArrayListIsEmpty("顺序表为空");
        }
        for (int i = 0; i < this.UseSize; i++) {
            System.out.print(elems[i] + " ");
        }
        System.out.println();
    }
        // 新增元素,默认在数组最后新增
        public void add(int data) {
            CheckUseSize();
            this.elems[this.UseSize] = data;
            this.UseSize++;

        }
        //判断pos是否合法
    private boolean Pos(int pos){
        return (pos<0||pos>=this.UseSize);
    }

        // 在 pos 位置新增元素
        public void add(int pos, int data) {
            CheckUseSize();
         if(!Pos(pos)){
             throw new ArrayIndexOutOfBoundsException("pos 位置不合法");
         }else {
             for(int i = this.UseSize-1;i>=pos;i--){
                 this.elems[i+1] = this.elems[i];
             }
             this.elems[pos] = data;
             this.UseSize++;
         }

        }
        // 判定是否包含某个元素
        public boolean contains(int toFind) {
            if(isEmpty()){
                for(int i = 0;i<this.UseSize;i++){
                    if(toFind==this.elems[i]){
                        return true;
                    }
                }
                return false;
            }else{
                throw new ArrayListIsEmpty("顺序表为空");
            }

        }
        // 查找某个元素对应的位置
        public int indexOf(int toFind) {
            if(isEmpty()){
                for(int i = 0;i<this.UseSize;i++){
                    if(toFind==this.elems[i]){
                        return i;
                    }
                }
                return -1;
            }
            throw new ArrayListIsEmpty("顺序表为空");

        }
        // 获取 pos 位置的元素
        public int get(int pos) {
        if(isEmpty()){
            throw new ArrayListIsEmpty("顺序表为空");

        }
            if(!Pos(pos)){
                throw new ArrayIndexOutOfBoundsException("pos 位置不合法");
            }else{
            return this.elems[pos];
        }
    }
        // 给 pos 位置的元素设为 value
        public void set(int pos, int value) {
            if(isEmpty()){
                throw new ArrayListIsEmpty("顺序表为空");
            }
            if(!Pos(pos)){
                throw new ArrayIndexOutOfBoundsException("pos 位置不合法");
            }else{
                this.elems[pos] = value;
            }
        }


        //删除第一次出现的关键字key
        public void remove(int toRemove) {

        }
        // 获取顺序表长度
        public int size() {
            if(isEmpty()){
                return 0;
            }
         return this.UseSize;

    }
        // 清空顺序表
        public void clear() {
            this.elems = null;
            this.UseSize = 0;

        }


        public static void main(String[] args) {
            TextDemo textDemo = new TextDemo();
            textDemo.add(1);
            textDemo.add(2);
            textDemo.add(3);
            textDemo.add(4);
            textDemo.add(5);
            textDemo.add(6);
            textDemo.display();
            textDemo.add(100,7);
            textDemo.display();


        }

    }

